﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JobsBoardDAL.DTOs;
using System.Windows;
using JobsBoardDAL;

namespace JobBoardAdminPanel.ViewModels
{
    public class CityViewModel : ViewModelBase
    {
        CityDTO city;

        public CityViewModel(CityDTO city)
        {
            this.city = city;
        }

        public int Id
        {
            get
            {
                return city.ID;
            }
        }

        public string CityName
        {
            get
            {
                return city.CityName;
            }
            set
            {
                if (this.city.CityName != value)
                {
                    this.city.CityName = value;
                    OnPropertyChanged("CityName");
                }
            }
        }

        internal void Update()
        {
            if (String.IsNullOrEmpty(CityName) || CityName.Length > 50)
            {
                MessageBox.Show("Invalid name.");
                return;
            }

            try
            {
                JobsBoardCRUD.EditCity(this.city);
            }
            catch
            {
                MessageBox.Show("Update unseccesful");
                return;
            }
        }

        internal void Delete()
        {
            try
            {
                JobsBoardCRUD.DeleteCity(this.city);
            }
            catch
            {
                MessageBox.Show("Update unseccesful. Other objects are using this one.");
                return;
            }
        }

        internal void Add(string countryName)
        {
            if (String.IsNullOrEmpty(CityName) || CityName.Length > 50)
            {
                MessageBox.Show("Invalid name.");
                return;
            }

            try
            {
                JobsBoardCRUD.AddCity(this.city, countryName);
                this.CityName = null;
            }
            catch
            {
                MessageBox.Show("Update unseccesful");
                return;
            }
        }
    }
}
